import { computed } from "vue";
import { EleMessage } from "ele-admin-plus/es";
import { storeToRefs } from "pinia";
import { useUserStore } from "@/store/modules/user";
import { listDictionaryData } from "@/api/system/dictionary-data";
import { searchListTypes, searchListExports } from "@/api/base/search";

/**
 * 获取字典数据hook
 * @param codes 字典编码
 */
export function useDictData(codes) {
  const result = [];

  // 已缓存的字典
  const userStore = useUserStore();
  const { dicts } = storeToRefs(userStore);

  codes.forEach((code) => {
    result.push(computed(() => dicts.value[code] || []));
    // 若还未缓存过则获取字典数据
    if (dicts.value[code] != null) {
      return;
    }
    userStore.setDicts([], code);
    listDictionaryData({ dict_code: code })
      .then((list) => {
        // 对 dict_data_code 进行类型转换处理
        const processedList = list.map((d) => ({
          ...d,
          dict_data_code: isNaN(d.dict_data_code)
            ? d.dict_data_code
            : parseInt(d.dict_data_code),
        }));
        userStore.setDicts(processedList, code);
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  });

  return result;
}

/**
 * 获取字典数据hook
 * @param codes 字典编码
 */
export function useDictTypeData(codes) {
  const result = [];

  // 已缓存的字典
  const userStore = useUserStore();
  const { typedicts } = storeToRefs(userStore);

  codes.forEach((code) => {
    result.push(computed(() => typedicts.value[code] || []));
    // 若还未缓存过则获取字典数据
    if (typedicts.value[code] != null) {
      return;
    }
    userStore.setTypeDicts([], code);
    searchListTypes({ type: code })
      .then((list) => {
        // 对 dict_data_code 进行类型转换处理
        const processedList = list.map((d) => ({
          ...d,
          label_value: isNaN(d.label_value)
            ? d.label_value
            : parseInt(d.label_value),
        }));
        userStore.setTypeDicts(processedList, code);
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  });

  return result;
}

/**
 * 获取字典数据hook
 * @param codes 字典编码
 */
export function useDictExportData(codes, reflush = false) {
  const result = [];

  // 已缓存的字典
  const userStore = useUserStore();
  const { exportdicts } = storeToRefs(userStore);

  codes.forEach((code) => {
    result.push(computed(() => exportdicts.value[code] || []));
    // 若还未缓存过则获取字典数据
    if (exportdicts.value[code] != null && !reflush) {
      return;
    }
    userStore.setExportDicts([], code);
    searchListExports({ table_name: code })
      .then((list) => {
        // 对 dict_data_code 进行类型转换处理
        const processedList = list.map((d) => ({
          ...d,
          label_value: isNaN(d.label_value)
            ? d.label_value
            : parseInt(d.label_value),
        }));
        userStore.setExportDicts(processedList, code);
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  });

  return result;
}
